package com.toe.commodity.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.commodity.service.CommodityGoodsService;
import com.toe.commodity.webapi.mapper.CommodityGoodsSpecificationsMapper;
import com.toe.common.restful.JsonPage;
import com.toe.common.utils.IpUtils;
import com.toe.pojo.entity.commodity.CommodityGoods;
import com.toe.commodity.webapi.mapper.CommodityGoodsMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.entity.commodity.CommodityGoodsSpecifications;
import com.toe.pojo.vo.commodity.*;
import com.toe.pojo.vo.order.QueryGoodsAndSpecifications;
import com.toe.pojo.vo.recommend.RecommendCommodityVO;
import com.toe.user.service.UserHistoryService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.SocketException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@DubboService
@GlobalTransactional
public class CommodityGoodsServiceImpl extends ServiceImpl<CommodityGoodsMapper, CommodityGoods> implements CommodityGoodsService {

    @Autowired
    CommodityGoodsMapper commodityGoodsMapper;
    @Autowired
    CommodityGoodsSpecificationsMapper commodityGoodsSpecificationsMapper;

    @DubboReference
    private UserHistoryService dubboUserHistoryService;

    @Value("${server.port}")
    private String port;

    @Value("${file.staticGoodsImgPatternPath}")
    private String staticGoodsImgPatternPath;

    @Override
    public void addNewCommodityGoods(AddNewGoods addNewGoods) {
        CommodityGoods goods = new CommodityGoods();
        goods.setTitle(addNewGoods.getTitle());
        goods.setIntroduce(addNewGoods.getIntroduce());
        goods.setTypeId(addNewGoods.getTypeId());
        goods.setTypeId(addNewGoods.getTypeId());
        goods.setCover(addNewGoods.getCover());
        goods.setBusinessId(addNewGoods.getBusinessId());
        goods.setSales(addNewGoods.getSales());
        goods.setStock(addNewGoods.getStock());
        commodityGoodsMapper.addNewCommodityGoods(goods);
        CommodityGoodsSpecifications specifications = new CommodityGoodsSpecifications();
        List<CommodityGoodsSpecificationsVO> list = addNewGoods.getSpecificationsList();
        for (CommodityGoodsSpecificationsVO commodityGoodsSpecificationsVO : list) {
            specifications.setGoodsId(goods.getId());
            specifications.setSpecifications(commodityGoodsSpecificationsVO.getSpecifications());
            specifications.setPrice(commodityGoodsSpecificationsVO.getPrice());
            commodityGoodsSpecificationsMapper.addNewSpecifications(specifications);
        }
    }

    @Override
    public void updateCommodityGoods(CommodityGoods commodityGoods) {
        commodityGoodsMapper.updateCommodityGoods(commodityGoods);
    }

    @Override
    public void deletedCommodityGoods(Long id) {
        commodityGoodsMapper.deletedCommodityGoods(id);
    }

    @Override
    public CommodityGoodsDetailsVO setHistoryGoodsById(Long id, Long userId) {
        CommodityGoodsVO goods = commodityGoodsMapper.getGoodsById(id);
        List<CommodityGoodsSpecificationsVO> specificationsList = commodityGoodsSpecificationsMapper.getSpecificationsList(id);
        if(userId != 0) {
            dubboUserHistoryService.insertHistory(userId, goods.getId(), 0);
        }
        CommodityGoodsDetailsVO goodsDetails = new CommodityGoodsDetailsVO();
        goodsDetails.setId(goods.getId());
        goodsDetails.setTitle(goods.getTitle());
        goodsDetails.setIntroduce(goods.getIntroduce());
        goodsDetails.setTypeId(goods.getTypeId());
        goodsDetails.setTypeName(goods.getTypeName());
        goodsDetails.setCover(goods.getCover());
        goodsDetails.setSales(goods.getSales());
        goodsDetails.setStock(goods.getStock());
        goodsDetails.setBusinessId(goods.getBusinessId());
        goodsDetails.setSpecificationsList(specificationsList);
        return goodsDetails;
    }

    @Override
    public ProductDetails getGoodsById(Long id, Long specificationsId) {
        CommodityGoodsSpecificationsVO specifications = commodityGoodsSpecificationsMapper.getSpecificationsById(specificationsId);
        CommodityGoodsVO goods = commodityGoodsMapper.getGoodsById(id);
        ProductDetails details = new ProductDetails();
        details.setId(goods.getId());
        details.setTitle(goods.getTitle());
        details.setIntroduce(goods.getIntroduce());
        details.setTypeId(goods.getTypeId());
        details.setTypeName(goods.getTypeName());
        details.setCover(goods.getCover());
        details.setStock(goods.getStock());
        details.setBusinessId(goods.getBusinessId());
        details.setSpecifications(specifications.getSpecifications());
        details.setPrice(specifications.getPrice());
        return details;
    }

    @Override
    public JsonPage<GoodsListDetailsVO> getCommodityGoodsBySales(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsListDetailsVO> commodityGoodsBySales = commodityGoodsMapper.getCommodityGoodsBySales();
        return JsonPage.restPage(new PageInfo<>(commodityGoodsBySales));
    }

    @Override
    public JsonPage<RecommendRandomVO> getRecommendRandom(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RecommendRandomVO> commodityGoodsBySales =
                commodityGoodsMapper.getRecommendRandom();
        return JsonPage.restPage(new PageInfo<>(commodityGoodsBySales));
    }

    @Override
    public List<RecommendCommodityVO> getCommodityGoodsByIds(List<Long> ids) {
        System.out.println("=========开始查询商品(商品模块)==========");
        return commodityGoodsMapper.getCommodityGoodsById(ids);
    }

    @Override
    public CommodityOrderGoodsVO getOrderGoodsById(Long goodsId, Long specificationId) {
        CommodityOrderGoodsVO goods = commodityGoodsMapper
                .getOrderGoodsById(goodsId, specificationId);
        goods.setCover(goods.getCover());
        System.out.println(goods.getCover());
        return goods;
    }

    @Override
    public List<CommodityOrderGoodsVO> listOrderGoodsByIds(
            List<QueryGoodsAndSpecifications> ids) {
        List<CommodityOrderGoodsVO> list = commodityGoodsMapper.listOrderGoodsByIds(ids);
        for (CommodityOrderGoodsVO commodityOrderGoodsVO : list) {
            commodityOrderGoodsVO.setCover(commodityOrderGoodsVO.getCover());
        }
        return list;
    }

    @Override
    public JsonPage<GoodsListDetailsVO> getCommodityGoodsByGoodsTypeId(Integer pageNum, Integer pageSize, Long id) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsListDetailsVO> commodity = commodityGoodsMapper.getCommodityGoodsByGoodsTypeId(id);
        for (GoodsListDetailsVO goodsListDetailsVO : commodity) {
            goodsListDetailsVO.setCover(goodsListDetailsVO.getCover());
        }
        return JsonPage.restPage(new PageInfo<>(commodity));
    }

    @Override
    public JsonPage<GoodsListDetailsVO> getCommodityGoodsByBusinessId(Integer pageNum, Integer pageSize, Long id) {
        System.out.println("进来了");
        PageHelper.startPage(pageNum, pageSize);
        System.out.println("分页成功");
        List<GoodsListDetailsVO> commodity = commodityGoodsMapper.getCommodityGoodsByBusinessId(id);
        for (GoodsListDetailsVO goodsListDetailsVO : commodity) {
            goodsListDetailsVO.setCover(goodsListDetailsVO.getCover());
        }
        System.out.println("查询成功");
        return JsonPage.restPage(new PageInfo<>(commodity));
    }

//    private String getUrl(String cover) {
//        String url = null;
//        String staticPath = staticGoodsImgPatternPath.split("\\*\\*")[0];
//
//        try {
//            url = "http://"+ IpUtils.getLocalIp() + ":" + port
//                    + staticPath + cover;
//        } catch (SocketException e) {
//            e.printStackTrace();
//        }
//
//        return url;
//    }

}
